package com.cbwl.eoas.cms.web.controller.sysconfig;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cbwl.eoas.cms.api.sysconfig.IDictDetailService;
import com.cbwl.eoas.cms.api.sysconfig.IDictTypeService;
import com.cbwl.eoas.cms.model.sysconfig.DictDetailDTO;
import com.cbwl.eoas.common.cache.redis.RedisKeyPrefix;
import com.cbwl.eoas.common.cache.redis.RedisManager;
import com.cbwl.eoas.common.framework.session.SessionManager;
import com.cbwl.eoas.common.framework.vo.PageRequest;
import com.cbwl.eoas.common.util.constain.DicTypeConstant;
import com.cbwl.eoas.common.web.Result;
import com.github.pagehelper.PageInfo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;



/**
 * 
 * <p>[描述信息：字典明细管理Controller]</p>
 *
 * @author 虞云波 - yuyunbo@3856.cc
 * @version 1.0 Created on 2017年10月12日 下午13:29:23
 */
@Controller
@RequestMapping("/dictdetail")
public class DictDetailController {
	
	//业务层注解
	@Autowired
	private IDictDetailService dictDetailService;
	
	
	@Autowired
	private IDictTypeService dictTypeService;
	
	@Autowired
	private  RedisManager redisManager;
	
	
	private static Logger logger = LoggerFactory.getLogger(DictDetailController.class);
	
	/**
	 * <p>初始化界面</p>
	 * 
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/initDictDetail")
	public String initDictDetailPage(HttpServletRequest request) {
		
		//查询字典大类
		request.setAttribute("beanList", dictTypeService.queryObjectList(null));
		
		return "initDictDetail";
	}


	/**
	 * <p>初始化新增修改界面</p>
	 * 
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/initAddUpdateDictDetailPage")
	public String initAddUpdateDictDetailPage(HttpServletRequest request,DictDetailDTO bean) {
		request.setAttribute("bean", bean);
		//查询字典大类
		request.setAttribute("beanList", dictTypeService.queryObjectList(null));
		

		DictDetailDTO detailBean = new DictDetailDTO();
		detailBean.setDictTypeNo(DicTypeConstant.state);
		request.setAttribute("detailBean", dictDetailService.queryObjectList(detailBean));
		
		return "initAddUpdateDictDetailPage";
	}
	

	/**
	 * <p>查询（分页）</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/queryDictDetailPage")
	@ResponseBody
	public Object queryDictDetailPage(HttpServletRequest request,DictDetailDTO bean,PageRequest pageRequest) {
		
		Result result = new Result(Result.Status.OK, "查询成功", "200");
		
		// 分页查询用户信息
		PageInfo<?> userPageInfo =  dictDetailService.queryPageList(bean, pageRequest);
		result.setData(userPageInfo);
		
		return result;
	}

	/**
	 * 
	 * <p>通过id查询主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/getDictDetailById")
	@ResponseBody
	public Object getDictDetailById(HttpServletRequest request,DictDetailDTO bean) {
		return dictDetailService.getObjectById(bean);
	}

	/**
	 * 
	 * <p>保存主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/saveDictDetail")
	@ResponseBody
	public Object saveDictDetail(HttpServletRequest request,DictDetailDTO bean) throws Exception {
		bean.setGmtCreate(new Date());
		bean.setCreater(SessionManager.getCurrentUser().getId());
		logger.info("{}新建了字典明细，字典编号{}", SessionManager.getCurrentUser().getEmpName(), bean.getDictNo());
		return dictDetailService.saveObject(bean);
	}

	/**
	 * 
	 * <p>更新主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 */
	@RequestMapping("/updateDictDetail")
	@ResponseBody
	public Object updateDictDetail(HttpServletRequest request,DictDetailDTO bean) throws Exception {		
		bean.setGmtModified(new Date());
		bean.setModifier(SessionManager.getCurrentUser().getId());
		logger.info("{}修改了字典明细，字典编号{}", SessionManager.getCurrentUser().getEmpName(), bean.getDictNo());
		return dictDetailService.updateObject(bean);
	}

	/**
	 * 
	 * <p>删除主体信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author 虞云波 - yuyunbo@3856.cc 
	 * @version 1.0 Created on 2017年10月12日 下午13:29:23
	 * @throws Exception 
	 */
	@RequestMapping("/deleteDictDetail")
	@ResponseBody
	public Object deleteDictDetail(HttpServletRequest request,DictDetailDTO bean) throws Exception {
		logger.info("{}删除了字典明细，字典编号{}", SessionManager.getCurrentUser().getEmpName(), bean.getDictNo());
		return dictDetailService.deleteObject(bean);
	}
	
	
	/**
	 * 
	 * <p>刷新redis缓存</p>
	 * 
	 * @param request
	 * @param bean
	 * @return
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年11月9日 下午2:22:15
	 */
	@RequestMapping("/fushDictToRedis")
	@ResponseBody
	public Object fushDictToRedis(HttpServletRequest request,DictDetailDTO bean){
		List<DictDetailDTO> list = dictDetailService.queryObjectList(bean);
		JedisPool jedisPool = redisManager.getJedisPool();
		Jedis jedis =jedisPool.getResource();
		
		Pipeline  pipeline = jedis.pipelined();
		list.forEach(dto ->{
			pipeline.set(RedisKeyPrefix.DIC_PREFIX+dto.getDictNo(),dto.getDictName());
		});
		pipeline.sync();
		jedisPool.returnResourceObject(jedis);
		return 33;
	}
	
	
	
}
